class TimeMachineUtil {
  /**
   * 获取某一年的第一个月的第一天和最后一个月的最后一天
   */

  static getStartEndYearDate(int iYear) {
    Map mapDate = new Map();
    int yearNow = DateTime.now().year;
    yearNow = yearNow + iYear;

    String newStartYear = '$yearNow' + '-' + '01' + '-' + '01';
    String newEndtYear = (yearNow + 1).toString() + '-' + '01' + '-' + '00';

    mapDate['startTime'] = TimeMachineUtil.formatDate(
        DateTime.fromMillisecondsSinceEpoch(turnTimestamp(newStartYear)),
        'yyyy-MM-dd');

    mapDate['endTime'] = TimeMachineUtil.formatDate(
        DateTime.fromMillisecondsSinceEpoch(turnTimestamp(newEndtYear)),
        'yyyy-MM-dd');

    mapDate['startStamp'] = turnTimestamp(mapDate['startTime'] + ' 00:00:00');

    mapDate['endStamp'] = turnTimestamp(mapDate['endTime'] + ' 23:59:59');
    print('某一年初和年末：$mapDate');
    return mapDate;
  }

  /**
   * 获得当前日期 未来/过去 第某个月第一天和最后一天时间
   */
  static Map<String, String> getMonthDate(int iMonth) {
    //获取当前日期
    var currentDate = new DateTime.now();
    if (iMonth + currentDate.month > 0) {
      return timeConversion(
          iMonth + currentDate.month, (currentDate.year).toString());
    } else {
      int beforeYear = (iMonth + currentDate.month) ~/ 12;
      String yearNew = (currentDate.year + beforeYear - 1).toString();
      int monthNew = (iMonth + currentDate.month) - beforeYear * 12;
      return timeConversion(12 + monthNew, yearNew);
    }
  }

  static Map<String, String> timeConversion(int monthTime, String yearTime) {
    Map<String, String> dateMap = Map();
    dateMap['startDate'] = '$yearTime' +
        '-' +
        (monthTime < 10 ? '0' + monthTime.toString() : '$monthTime') +
        '-' +
        '01';
    //转时间戳再转时间格式 防止出错
    dateMap['startDate'] = TimeMachineUtil.formatDate(
        DateTime.fromMillisecondsSinceEpoch(
            turnTimestamp(dateMap['startDate'])),
        'yyyy-MM-dd');

    //某个月结束时间，转时间戳再转
    String endMonth = '$yearTime' +
        '-' +
        ((monthTime + 1) < 10
                ? '0' + (monthTime + 1).toString()
                : (monthTime + 1))
            .toString() +
        '-' +
        '00';
    var endMonthTimeStamp = turnTimestamp(endMonth);
    endMonth = TimeMachineUtil.formatDate(
        DateTime.fromMillisecondsSinceEpoch(endMonthTimeStamp), 'yyyy-MM-dd');
    dateMap['endDate'] = endMonth;
    //这里是为了公司后台接口 需加时间段的时间戳 但不显示在格式化实践中
    dateMap['startDateStamp'] =
        turnTimestamp(dateMap['startDate'] + ' 00:00:00').toString();

    dateMap['endDateStamp'] =
        turnTimestamp(dateMap['endDate'] + ' 23:59:59').toString();
    return dateMap;
  }

  /**
   * 转时间戳
   */
  static int turnTimestamp(String timestamp) {
    print("timestamp:$timestamp");
    return DateTime.parse(timestamp).millisecondsSinceEpoch;
  }

  /**
   * 当前时间 过去/未来 某个周的周一和周日
   */
  static Map<String, String> getWeeksDate(int weeks) {
    Map<String, String> mapTime = new Map();
    DateTime now = new DateTime.now();
    int weekday = now.weekday; //今天周几

    var sunDay = getTimestampLatest(false, 7 - weekday + weeks * 7); //周末
    var monDay = getTimestampLatest(true, -weekday + 1 + weeks * 7); //周一

    mapTime['monDay'] = TimeMachineUtil.formatDate(
        DateTime.fromMillisecondsSinceEpoch(sunDay), 'yyyy-MM-dd'); //周一 时间格式化
    mapTime['sunDay'] = TimeMachineUtil.formatDate(
        DateTime.fromMillisecondsSinceEpoch(monDay), 'yyyy-MM-dd'); //周一 时间格式化
    mapTime['monDayStamp'] = '$monDay'; //周一 时间戳
    mapTime['sunDayStamp'] = '$sunDay'; //周日 时间戳
    print('某个周的周一和周日：$mapTime');
    //某个周的周一和周日：{monDay: 2022-02-20, sunDay: 2022-02-14, monDayStamp: 1644768000000, sunDayStamp: 1645372799000}
    return mapTime;
  }

  /**
   * phase : 是零点还是23:59:59
   */
  static int getTimestampLatest(bool phase, int day) {
    String newHours;
    DateTime now = new DateTime.now();
    DateTime sixtyDaysFromNow = now.add(new Duration(days: day));
    String formattedDate =
        TimeMachineUtil.formatDate(sixtyDaysFromNow, 'yyyy-MM-dd');
    if (phase) {
      newHours = formattedDate + ' 00:00:00';
    } else {
      newHours = formattedDate + ' 23:59:59';
    }

    DateTime newDate = DateTime.parse(newHours);
    String newFormattedDate =
        TimeMachineUtil.formatDate(newDate, 'yyyy-MM-dd HH:mm:ss');
    int timeStamp = newDate.millisecondsSinceEpoch;
    // print('时间' + newFormattedDate);
    return timeStamp;
  }

  // 手动拼接时间
  static String formatDate(DateTime dateTime, String format) {
    if (dateTime == null) return '';
    // format = format ?? DateFormats.full;
    if (format.contains('yy')) {
      String year = dateTime.year.toString();
      if (format.contains('yyyy')) {
        format = format.replaceAll('yyyy', year);
      } else {
        format = format.replaceAll(
            'yy', year.substring(year.length - 2, year.length));
      }
    }

    format = _comFormat(dateTime.month, format, 'M', 'MM');
    format = _comFormat(dateTime.day, format, 'd', 'dd');
    format = _comFormat(dateTime.hour, format, 'H', 'HH');
    format = _comFormat(dateTime.minute, format, 'm', 'mm');
    format = _comFormat(dateTime.second, format, 's', 'ss');
    format = _comFormat(dateTime.millisecond, format, 'S', 'SSS');

    return format;
  }

  /// com format.
  static String _comFormat(
      int value, String format, String single, String full) {
    if (format.contains(single)) {
      if (format.contains(full)) {
        format =
            format.replaceAll(full, value < 10 ? '0$value' : value.toString());
      } else {
        format = format.replaceAll(single, value.toString());
      }
    }
    return format;
  }
}
